//
//  PsdTradeCodeVerify.swift
//  LeGou
//
//  Created by lxm on 2022/8/1.
//

import UIKit

class TradePwdSet: ZMViewController, XMCodeInputViewDelegate {
    
    //MARK:--------------------属性--------------------
    @IBOutlet weak var codeView: XMCodeInputView!
    
    // 0 验证旧交易密码
    // 1 输入交易米密码
    // 2 再次输入交易密码
    var type = 0
    
    // 是否是找回
    var find = true
    
    var pa = [String : Any]()
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var loginBtn: UIButton!
    
    @IBOutlet weak var titleLabel: UILabel!
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "修改密码"
        codeView.inputCount = 6
        codeView.space = 8
        let con = XMCodeInputCellConfig()
        con.hideLine = true
        con.borderColor = "#EAEAEA".zm.hex()
        con.borderWidth = 1
        con.securityInput = true
        con.cornerRadius = 8
        con.backGourndColor = .white
        codeView.cellConfig = con
        codeView.delegate = self
        codeView.reaload()
        codeView.collectionView.backgroundColor = "#F5F5F5".zm.hex()
        codeView.becomeFirstResponder()
        
        if type == 0 {
            titleLabel.text = "请输入原支付密码"
        } else if type == 1 {
            titleLabel.text = "请设置支付密码 用于支付验证"
        } else if type == 2 {
            titleLabel.text = "请再次输入以确认"
            loginBtn.setTitle("确定", for: .normal)
        }
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        codeView.textField.rx.text.orEmpty.map({$0.count == 6}).subscribe(onNext:{ [unowned self] resp in
            self.loginBtn.isEnabled = resp
            self.loginBtn.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }
    
    
    @IBAction func onNext(_ sender: Any) {
        if type == 0 {
            let vc = TradePwdSet()
            vc.type = 1
            vc.pa["oldTradePwd"] = codeView.text
            vc.find = find
            replace(vc, true)
        } else if type == 1{
            let vc = TradePwdSet()
            pa["tradePwd"] = codeView.text
            vc.pa = pa
            vc.type = 2
            vc.find = find
            self.navigationController?.pushViewController(vc, animated: true)
        } else if type == 2{
            if (codeView.text != (pa["tradePwd"] as? String)) {
                ZMToast.showToast(title: "两次密码输入不一致")
                return
            }
            let api = find ? MineAPI.findTradePwd(pa) : MineAPI.changeTradePwd(pa)
            api.rxRequest(type: Any.self).subscribe(onNext:{ [unowned self] resp in
                ZMAppShareData.share.requestUserInfo { ok in
                    
                }
                if let toVCType = MineService.shared.tradeSuccessToVC {
                    _ = ZMJumpHelper.jumpToClass(VC: self, toClass: toVCType.classForCoder(), notContainAutoJump: true)
                    ZMToast.showToast(title: "交易密码设置成功")
                    MineService.shared.tradeSuccessToVC = nil
                } else {
                    if ZMAppShareData.share.safeUserInfo.isTradeOk {
                        ZMToast.showToast(title: "交易密码修改成功")
                        _ = ZMJumpHelper.jumpToClass(VC: self, toClass: TradePwdVC.classForCoder(), notContainAutoJump: true)
                    } else {
                        ZMToast.showToast(title: "交易密码设置成功")
                        _ = ZMJumpHelper.jumpToClass(VC: self, toClass: SettingVC.classForCoder(), notContainAutoJump: true)
                    }
                }
                
            }).disposed(by: disposeBag)
        }
    }

    //MARK:--------------------delegate--------------------
    func inputComplete(_ text: String) {
        print(text)
    }
}
